home *** CD-ROM | disk | FTP | other *** search
/ User's Choice Windows CD / User's Choice Windows CD (CMS Software)(1993).iso / misc1 / iv26_w30.zip / EXAMPLES / LOGO / LOGO.C next >
C/C++ Source or Header  |  1991-12-21  |  6KB  |  179 lines

  1.  
  2. /*
  3.  * Copyright (c) 1989 Stanford University
  4.  *
  5.  * Permission to use, copy, modify, distribute, and sell this software and its
  6.  * documentation for any purpose is hereby granted without fee, provided
  7.  * that the above copyright notice appear in all copies and that both that
  8.  * copyright notice and this permission notice appear in supporting
  9.  * documentation, and that the name of Stanford not be used in advertising or
  10.  * publicity pertaining to distribution of the software without specific,
  11.  * written prior permission.  Stanford makes no representations about
  12.  * the suitability of this software for any purpose.  It is provided "as is"
  13.  * without express or implied warranty.
  14.  *
  15.  * STANFORD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
  16.  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
  17.  * IN NO EVENT SHALL STANFORD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  18.  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
  19.  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  20.  * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
  21.  * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  */
  23.  
  24. /*
  25.  * Logo object implementation.
  26.  */
  27.  
  28. #include "logo.h"
  29. #include <InterViews/bitmap.h>
  30. #include <InterViews/painter.h>
  31. #include <InterViews/pattern.h>
  32. #include <InterViews/sensor.h>
  33. #include <InterViews/shape.h>
  34. #include <InterViews/transformer.h>
  35. #include <InterViews/color.h>
  36. #include <math.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <stdlib.h>
  40.  
  41. Logo::Logo () { 
  42.     SetClassName("Logo");
  43.     input = new Sensor();
  44.     input->Catch(KeyEvent);
  45.  
  46.     bitmap = nil;
  47.     rainbow = 0;
  48. }
  49.  
  50. void Logo::Handle (Event& e) {
  51.     if (e.eventType == KeyEvent && e.keystring[0] == 'q') {
  52.         e.target = nil;
  53.     }    
  54. }
  55.  
  56. void Logo::Reconfig () {
  57.     const char* name = GetAttribute("bitmap");
  58.     rainbow = atoi(GetAttribute("rainbow"));
  59.  
  60.     if (bitmap == nil) {
  61.         if (name != nil) {
  62.             bitmap = new Bitmap(name);
  63.         }
  64.     }
  65.     if (bitmap != nil) {
  66. //        if (bitmap->Valid()) {
  67.             shape->width = bitmap->Width();
  68.             shape->height = bitmap->Height();
  69. //        } else {
  70. //            fprintf(stderr, "logo: cannot open bitmap file `%s'\n", name);
  71. //            exit(1);
  72. //        }
  73.     } else {
  74.         shape->width = 65;
  75.         shape->height = 65;
  76.     }
  77. }
  78.  
  79. void Logo::DrawBitmap () {
  80.     Transformer* oldt = output->GetTransformer();
  81.  
  82.     Transformer t;
  83.     t.Scale(float(xmax+1)/bitmap->Width(), float(ymax+1)/bitmap->Height());
  84.  
  85.     output->SetTransformer(&t);
  86.     output->Stencil(
  87.         canvas, -bitmap->Left(), -bitmap->Bottom(), bitmap, bitmap
  88.     );
  89.  
  90.     output->SetTransformer(oldt);
  91.     output->SetOrigin(0, 0);
  92. }
  93.  
  94. static Coord poly1_x[] = {   0, 245, 200, 350, 450,  50,  50, 100,   0};
  95. static Coord poly1_y[] = {  71, 314, 350, 350, 450, 450, 310, 310, 210};
  96. static int poly1_count = sizeof(poly1_x)/sizeof(Coord);
  97.  
  98. static Coord poly2_x[] = {   0, 550, 650,   0};
  99. static Coord poly2_y[] = { 550, 550, 650, 650};
  100. static int poly2_count = sizeof(poly2_x)/sizeof(Coord);
  101.  
  102. void do_draw (
  103.     Painter* painter, Canvas* canvas, float rotate, float scalex, float scaley
  104. ) {
  105.     Transformer* oldt = painter->GetTransformer();
  106.     Transformer t;
  107.     t.Rotate(rotate);
  108.     t.Scale(scalex, scaley);
  109.     painter->SetTransformer(&t);
  110.     painter->FillPolygon(canvas, poly1_x, poly1_y, poly1_count);
  111.     painter->FillPolygon(canvas, poly2_x, poly2_y, poly2_count);
  112.     painter->SetTransformer(oldt);
  113. }
  114.  
  115. void Logo::DrawPolygon () {
  116.     float scalex = float(xmax+2)/1300;
  117.     float scaley = float(ymax+2)/1300;
  118.  
  119.     output->SetOrigin(xmax/2, ymax/2);
  120.  
  121.     do_draw(output, canvas, 0, scalex, scaley);
  122.     do_draw(output, canvas, 90, scalex, -scaley);
  123.     do_draw(output, canvas, 90, scalex, scaley);
  124.     do_draw(output, canvas, 0, scalex, -scaley);
  125.     do_draw(output, canvas, 0, -scalex, -scaley);
  126.     do_draw(output, canvas, 90, -scalex, scaley);
  127.     do_draw(output, canvas, 90, -scalex, -scaley);
  128.     do_draw(output, canvas, 0, -scalex, scaley);
  129.  
  130.     output->SetOrigin(0, 0);
  131. }
  132.  
  133. static float red_profile[] =    { 1.0, 1.0, 1.0, 0.0, 0.0, 0.5, 1.0 };
  134. static float green_profile[] =  { 0.0, 0.5, 1.0, 0.8, 0.0, 0.0, 0.0 };
  135. static float blue_profile[] =   { 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0 };
  136. static int profile_count = sizeof(red_profile)/sizeof(float);
  137.  
  138. static unsigned intensity (float hue, float* profile, int count) {
  139.     int index = min(int(floor(hue * (count-1))), count-1);
  140.     float i = (
  141.         profile[index]
  142.         + (hue * (count-1) - index) * (profile[index+1] - profile[index])
  143.     );
  144.     return unsigned(i * 65535);
  145. }
  146.  
  147. static Color** colors;
  148.  
  149. void Logo::Redraw (Coord, Coord, Coord, Coord) {
  150.     if (rainbow > 0) {
  151.         if (colors == nil) {
  152.             colors = new Color* [rainbow];
  153.             for (int c = 0; c < rainbow; ++c) {
  154.                 float hue = float(c) / float(rainbow-1);
  155.                 colors[c] = new Color(
  156.                     intensity(hue, red_profile, profile_count),
  157.                     intensity(hue, green_profile, profile_count),
  158.                     intensity(hue, blue_profile, profile_count)
  159.                 );
  160.                 colors[c]->Reference();
  161.             }
  162.         }
  163.         float h = float(ymax+1)/rainbow;
  164.         Color* oldfg = output->GetFgColor();
  165.         for (int c = 0; c < rainbow; ++c) {
  166.             output->SetColors(colors[c], nil);
  167.             output->FillRect(canvas, 0, Coord(c * h), xmax, Coord((c+1) * h));
  168.         }
  169.         output->SetColors(oldfg, nil);
  170.     } else {
  171.         output->ClearRect(canvas, 0, 0, xmax, ymax);
  172.     }
  173.     if (bitmap == nil) {
  174.         DrawPolygon();
  175.     } else {
  176.         DrawBitmap();
  177.     }
  178. }
  179.